<?php

namespace XWAM\Controller;

/**
 * Links 控制器类
 *
 * @since 1.0 <2015-11-10> SoChishun <14507247@qq.com> Added.
 */
class Links extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        $where = array();
        if (!empty($search['word'])) {
            $where['title'] = array('like', '%' . $search['word'] . '%');
        }
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    function index() {
        $asearch = $this->indexSearch();
        $mLinks = new \XWAM\Model\LinksModel();
        $list = $mLinks->selectPaging($page, array('where' => $asearch['where']));
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->display();
    }

    function edit($id = 0) {
        $data = array();
        if ($id) {
            $mLinks = new \XWAM\Model\LinksModel();
            $data = $mLinks->find($id);
        }
        if (!$data) {
            $data = array('user_name' => $this->userName, 'site_id' => $this->siteId);
        }
        $this->assign('data', $data);
        $this->display();
    }

    function editSave() {
        $mLinks = new \XWAM\Model\LinksModel();
        $result = $mLinks->saveLink();
        $this->dialogJump($result['status'], $result['info']);
    }

    function changeStatus($id, $status) {
        $mLinks = new \XWAM\Model\LinksModel();
        $this->ajaxReturn($mLinks->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mLinks = new \XWAM\Model\LinksModel();
        $this->ajaxReturn($mLinks->changeSort($id, $sort));
    }

    function delete($id = '') {
        $mLinks = new \XWAM\Model\LinksModel();
        $result = $mLinks->deleteLink($id);
        $this->ajaxReturn($result);
    }

}
